package com.example.hms.servicelmpl.serviceImpl;

import com.example.hms.dao.UserDao;
import com.example.hms.entity.User;
import com.example.hms.servicelmpl.UserService;
import com.example.hms.util.Result;
import com.example.hms.util.ResultSubmit;
import com.example.hms.util.TokenUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Result login(String userId, String password, HttpServletRequest request) {
        User user = userDao.login(userId);
        if (user == null) {
            return ResultSubmit.error("登录失败,账号不存在");
        }
        if (password.equals(user.getUserPassword())){
            HttpSession session = request.getSession();
            session.setAttribute("userId", user.getUserId().toString());
            session.setAttribute("userName", user.getUserName());
            session.setAttribute("roleName", user.getRoleName());
            Long time = System.currentTimeMillis();
            session.setAttribute("time", time);
            session.setAttribute("token", TokenUtil.getToken(
                    String.valueOf(user.getUserId()),user.getRoleName(),time));
            return ResultSubmit.success(sweet(user));
        }else {
            return ResultSubmit.error("登录失败,密码错误");
        }
    }

    private User sweet (User user){
        User sweet = new User();
        sweet.setUserId(user.getUserId());
        sweet.setUserName(user.getUserName());
        sweet.setRoleName(user.getRoleName());
        return sweet;
    }

    @Override
    public Result info(HttpServletRequest request) {
        HttpSession session = request.getSession();
//        Map<String,String> map = new HashMap<>();
//        map.put("userId",String.valueOf(session.getAttribute("userId")));
//        map.put("userName",String.valueOf(session.getAttribute("userName")));
//        map.put("roleName",String.valueOf(session.getAttribute("roleName")));
//
//        return ResultSubmit.success(map);

        User user=new User();
        user.setUserId(Integer.valueOf((String) session.getAttribute("userId")));
        user.setUserName((String) session.getAttribute("userName"));
        user.setRoleName((String) session.getAttribute("roleName"));
        return ResultSubmit.success(user);
    }
    @Override
    public Result selectAllUser(String userId, String userName, String roleName){
        List<User> user = userDao.selectAllUser(userId,userName,roleName);
        return ResultSubmit.success(user);
    }

    @Override
    public Result insert(User user){
        user.setUserId(Math.abs((int)System.currentTimeMillis()));
        user.setDeleteIdentity("未删除");
        user.setCreateTime(format.format(new Date()));
        user.setRoleId(2);
        int i = userDao.insert(user);
        // todo 系统记录
        if (i == 1){
            return ResultSubmit.success("新增成功");
        }else {
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("新增失败");
        }
    }
    //修改
    @Override
    public Result update(User user){
        int i = userDao.update(user);
        user.setCreateTime(format.format(new Date()));
        user.setDeleteIdentity("未删除");
        int j = userDao.insert(user);
        //todo 系统记录操作
        if (i == 1 || j == 0){
            return ResultSubmit.success("成功");
        }else {
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("失败");
        }
    }
    //删除
    @Override
    public Result delete(String userId){
        User user = userDao.login(userId);
        int i = userDao.update(user);
        //todo 系统记录操作
        if (i == 1){
            return ResultSubmit.success("成功");
        }else {
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("失败");
        }
    }
}

